

(function() {
    var checkedDepart;

    function renderSelectDepart(container, obj){

        var html = '<div class="list-block"><ul>';

        if (obj.children.length > 0) {

            $$.each(obj.children, function(index, el){

                if(el.children.length > 0){

                    html += '<li class="accordion-item">'+
                        '<label data-departId="'+ el.id +'" class="label-checkbox">'+
                        '<input data-id="'+ el.id +'" data-label="'+ el.label +'" data-id="'+ el.id +'" type="checkbox" id="'+ el.id +'" name="" value="">'+
                        '<div class="item-media">'+
                        '<i class="icon icon-form-checkbox"></i>'+
                        '</div>'+
                        '</label>'+
                        '<a href="#" class="item-content item-link">'+
                        '<div class="item-inner">'+
                        '<div class="item-title">'+ el.label +'</div>'+
                        '</div>'+
                        '</a>'+
                        '<div class="accordion-item-content">';
                    html += renderSelectDepart(container, el);

                    html += '</div></li>';
                } else {
                    html += '<li class="disaccordion-item">'+
                        '<label data-departId="'+ el.id +'" class="item-content item-link label-checkbox">'+
                        '<input data-id="'+ el.id +'" data-label="'+ el.label +'" data-id="'+ el.id +'" type="checkbox" id="'+ el.id +'" name="" value="">'+
                        '<div class="item-media">'+
                        '<a href="#" class="link"><i class="icon icon-form-checkbox"></i></a>'+
                        '</div>'+

                        '<div class="item-inner">'+
                        '<div class="item-title">'+ el.label +'</div>'+
                        '</div>'+
                        '</label>';
                    html += "</li>"
                }

                bindCheckBoxChange(container, '[data-departId="' + el.id + '"]');

            });

        }

        html += '</ul></div>';

        return html;
    }

    function bindCheckBoxChange (container, selector) {

        $$(document).on("change", selector, $$.delayHandler(function (e){

            var checked = container.find(selector).find('input[type="checkbox"]').prop('checked');

            if (checked) {
                container.find(selector).parent("li").find('input[type="checkbox"]').prop('checked', true);
                container.find(selector).parent("li").find('input[data-id]').each(function(index, el){
                    pushCheckedDepart(el);
                });

                renderSelectArea(container)

            } else {
                container.find(selector).parent("li").find('input[type="checkbox"]').prop('checked', false);
                container.find(selector).parent("li").find('input[data-id]').each(function(index, el){
                    deleCheckedDepart(el);
                });

                renderSelectArea(container)
            }

        }));
    }

    function pushCheckedDepart (selector) {
        var departinfo = $$.dataset(selector);

        var key = departinfo.id;
        if(!checkedDepart[key]){
            checkedDepart[key] =  departinfo;
        }
    }

    function deleCheckedDepart (selector) {
        var departinfo = $$.dataset(selector);
        var key = departinfo.id;
        if(checkedDepart[key]){
            delete checkedDepart[key];
        }
    }

    function renderSelectArea (container) {
        var liHtml = '';
        $$.each(checkedDepart, function(key, el){

            container.find('[data-id="'+ key +'"]').parents('li').children('label').find('input[type="checkbox"]').prop('checked', true);
            container.find('[data-id="'+ key +'"]').find('input[type="checkbox"]').prop('checked', true);

            liHtml += '<li class="item">'+
                '<p>'+ el.label +'</p>'+
                '</li>';
        });
        container.find('.selectedDepartList').html(liHtml);
    }

    //选择组织部门页面
    myApp.onPageInit("selectDepart", function (page) {
        var pageCon = $$(page.container);
        var delegateName = $$.parseUrlQuery(page.url).delegate;
        var delegate = window[delegateName];
        window[delegateName] = undefined;

        checkedDepart = delegate && delegate.selectedDeparts;
        checkedDepart = checkedDepart || {};

        //选组织示例数据（2015-09-23）
        // var checkedDepart = {
        //     283768: {
        //         id: 283768,
        //         label: "秘书科"
        //     },
        //     296249: {
        //         id: 296249,
        //         label: "我的部门"
        //     }
        // }

        $$.getJSONP(SERVER_PATH + 'listdepart.do', null, function (json) {
            if (json.code == 1) {
                var addressBookData = json.results[0];
                var html = renderSelectDepart(pageCon, addressBookData);

                pageCon.find('.selectDepart').html(html);

                renderSelectArea(pageCon);

                pageCon.find(".selectedDepartArea .completeBtn").on("click", function(e){
                    delegate.delegate(checkedDepart);
                    page.view.back();
                });
            }
        });
    });


    myApp.onPageInit("selectDepart-pad", function (page) {
        var pageCon = $$(page.container);
        var delegateName = $$.parseUrlQuery(page.url).delegate;
        var delegate = window[delegateName];
        window[delegateName] = undefined;

        checkedDepart = delegate && delegate.selectedDeparts;
        checkedDepart = checkedDepart || {};

        var subpages = pageCon.find('.subpage-content');

        $$.getJSONP(SERVER_PATH + 'listdepart.do', null, function (json) {
            if (json.code == 1) {
                var addressBookData = json.results[0];
                var html = renderSelectDepart(pageCon, addressBookData);

                subpages.eq(0).html(html);

                renderSelectArea(pageCon);

                pageCon.find(".selectedDepartArea .completeBtn").on("click", function(e){
                    delegate.delegate(checkedDepart);
                    page.view.back();
                });
            }
        });
    });
}());

